﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;


namespace SharedGamesClasses {
    public static class Pig {

        public const int NUM_OF_PLAYERS = 2;
        public const int USER = 0;
        public const int COMPUTER = 1;
        public const int END_SQUARE = 30;
        
        // More constants and/or class variables may be needed.
        // 0: Player's turn
        // 1: Computer's turn
        private static int TURN = 0;
        public static int pointsCurrent = 0;
        private static int faceValue = 1;

        private static string[] playersName = new string[NUM_OF_PLAYERS] { "Player", "Computer" };

        public static int[] pointsTotal = new int[NUM_OF_PLAYERS] { 0, 0 };

        private static Die die = new Die();

        /// <summary>
        /// Reset all class variables to "zeroed" values,
        /// counters and flags but not the die.
        /// 
        /// used by both Console and GUI to ensure subsequent 
        /// playing of the game is correct
        /// </summary>
        public static void ResetGlobals() {
            pointsCurrent = 0;
            TURN = TURN == 0 ? 1 : 0;
           
        } // end Reset Globals



      
        /// <summary>
        /// Determines if the specified player (who) has won
        /// </summary>
        /// <param name="who"> a player</param>
        /// <returns> true if specified player has won, false otherwise</returns>
        public static bool HasWon(int who) {
            return (pointsTotal[who] >= END_SQUARE);
        } //end HasWon

        /// <summary>
        /// Plays the game of Pig  until the user decides to stop
        /// Used by Console version only
        /// </summary>
        public static void PlayConsole() {

            while (!HasWon(USER) && !HasWon(COMPUTER))
            {
                faceValue = die.Roll();

                Console.WriteLine("\n" + playersName[TURN] + " threw a " + faceValue + "\n");
                    
                if (faceValue == 1)
                {
                    Console.WriteLine("\n" + playersName[TURN] + " is at position " + pointsTotal[TURN] + "\n");
                    ResetGlobals();
                }
                else
                {
                    pointsCurrent += faceValue;
                    Console.WriteLine("\n" + playersName[TURN] + " is at position " + (pointsTotal[TURN] + pointsCurrent) + "\n");
                    ConsoleKeyInfo choosenKey;

                    do
                    {
                        Console.Write("\nDoes the " + playersName[TURN] + " want to Hold ? <Y or N>: ");
                        choosenKey = Console.ReadKey();
                        Console.WriteLine("\n");
                    }
                    while (choosenKey.Key != ConsoleKey.N && choosenKey.Key != ConsoleKey.Y);

                    if (choosenKey.Key == ConsoleKey.N)
                    {
                    }
                    else
                    {
                        pointsTotal[TURN] += pointsCurrent;                        
                        Console.WriteLine("\n" + playersName[TURN] + " is at position " + pointsTotal[TURN] + "\n");
                        ResetGlobals();
                    }
                }               
            }

            if (HasWon(USER))
            {
                Console.WriteLine("\n*********************************************\n");
                Console.WriteLine("\n     Player wins!\n");
                Console.WriteLine("\n*********************************************\n");
            }
            else
            {
                Console.WriteLine("\n*********************************************\n");
                Console.WriteLine("\n     Computer wins!\n");
                Console.WriteLine("\n*********************************************\n");
            }
        } //end PlayConsole


        /* The following method should be called by your GUI code.
         * I.e. Do not use it when writing your Console game,
         * but do not delete it. */

        /// <summary>
        /// Plays one roll of the die for a specified player (who).
        /// 
        /// Used by GUI version to roll a die once.
        /// </summary>
        /// <param name="who">a player</param>
        /// <param name="firstRoll"> true indicates it is the first roll of "who's" turn,
        /// false indicates it is a continuation of "who's" turn</param>
        /// <returns>true if player has not thrown a 1, otherwise false</returns>
        public static bool PlayGUI(int who, bool firstRoll) {
            bool result = true;
            
            // code to be inserted here
            faceValue = die.Roll();

            Trace.WriteLine("\n" + playersName[who] + " threw a " + faceValue + "\n");
            
            if (faceValue == 1)
            {
                Trace.WriteLine("\n" + playersName[who] + " is at position " + pointsTotal[who] + "\n");
                result = false;
            }
            else
            {
                if (firstRoll)
                {
                    ResetGlobals();
                }
                else
                {
                }

                pointsCurrent += faceValue;
                Trace.WriteLine("\n" + playersName[who] + " is at position " + (pointsTotal[who] + pointsCurrent) + "\n");                
            }
            // the following statement is only provided to ensure code compiles
            // it can be deleted should your logic permits it with appropriate 
            // placed return statements.    

            return result;
        }//end PlayGui

    } // end Pig class
}
